// TabCtrlEx.cpp : implementation file
/*********************************************************************
*	Author         : orbit
*	Date           : March 27, 2000
* Last-modified  : 
*	Contact us     : inte2000@163.com,support@winmsg.com
*	Web Page       : http://www.winmsg.com/cn/orbit.htm (for Chinese version)
*						       http://www.winmsg.com/orbit.htm (for English version)
**********************************************************************/
#include "stdafx.h"
#include "TabCtrlEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTabCtrlEx

CTabCtrlEx::CTabCtrlEx()
{
	m_crSelColour     = RGB(208,12,243);
	m_crUnselColour   = RGB(0,0,255);
}

CTabCtrlEx::~CTabCtrlEx()
{
}

BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
	//{{AFX_MSG_MAP(CTabCtrlEx)
	ON_WM_CREATE()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTabCtrlEx message handlers

int CTabCtrlEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTabCtrl::OnCreate(lpCreateStruct) == -1) 
		return -1;
	ModifyStyle(0, TCS_OWNERDRAWFIXED);
	
	return 0;
}

void CTabCtrlEx::PreSubclassWindow() 
{	
	ModifyStyle(0, TCS_OWNERDRAWFIXED);
	CTabCtrl::PreSubclassWindow();
}

void CTabCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{	
	CRect rect = lpDrawItemStruct->rcItem;
	int nTabIndex = lpDrawItemStruct->itemID;
	if (nTabIndex < 0) 
		return;
	
//	BOOL bSelected = (nTabIndex == GetCurSel());
	BOOL bSelected = (lpDrawItemStruct->itemState&ODS_SELECTED);
	
	char label[128];
	TC_ITEM tci;
	tci.mask = TCIF_TEXT|TCIF_IMAGE;
	tci.pszText = label;     
	tci.cchTextMax = 127;    	
	if (!GetItem(nTabIndex, &tci )) 
		return;

	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	if (!pDC) 
		return;
	int nSavedDC = pDC->SaveDC();

//	if(rect.top < 0)
//		rect.top = 0;

	pDC->SetBkMode(TRANSPARENT);
	pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));

	// Draw image
	CImageList* pImageList = GetImageList();
	if (pImageList && tci.iImage >= 0) 
	{
		rect.left += 2;
		IMAGEINFO info;
		pImageList->GetImageInfo(tci.iImage, &info);
		CRect ImageRect(info.rcImage);
		int nYpos = rect.top;

		pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
		rect.left += ImageRect.Width();
	}
		

	if (bSelected) 
		pDC->SetTextColor(m_crSelColour);
	else 
		pDC->SetTextColor(m_crUnselColour);

	pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
	
	pDC->RestoreDC(nSavedDC);
}

/////////////////////////////////////////////////////////////////////////////
// CTabCtrlEx operations

void CTabCtrlEx::SetColours(COLORREF bSelColour, COLORREF bUnselColour)
{
	m_crSelColour = bSelColour;
	m_crUnselColour = bUnselColour;
	Invalidate();
}

void CTabCtrlEx::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	TCHITTESTINFO hti;
	hti.pt = point;
	hti.flags = TCHT_ONITEM | TCHT_ONITEMICON | TCHT_ONITEMLABEL ;
	int nTabID = HitTest(&hti);

	GetParent()->PostMessage(MSG_TABCTRL_2_TABBARWND,nTabID,0);
	
	CTabCtrl::OnLButtonDblClk(nFlags, point);
}
